package org.itboys.portal.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.portal.entity.PortalInfoComment;
import org.itboys.portal.service.PortalInfoCommentService;
import org.itboys.portal.service.manager.MobiPortalInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-3-30 下午3:58:29 
 * 资讯评论管理 
 */
@RestController
@RequestMapping("/portal/info/comment")
public class PortalInfoCommentController extends BaseController {
	
	@Autowired
	private ResourceHolder resourceHolder;
	@Autowired
	private PortalInfoCommentService infoCommentService;
	@Autowired
	private MobiPortalInfoManager portalInfoManager;
	@Autowired
	private MemberService memberService;

	/**
	 * 跳往资讯或者产品的评论页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/commentVM/{id}")
	public ModelAndView commentVM(@PathVariable("id")Long id, 
			Model model){
		model.addAttribute("id", id);
		model.addAttribute("staticRoot",
				resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",
				resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot", resourceHolder.getStringValue("imgRoot"));
		return new ModelAndView("portal/info/commentList");
	}
	
	/**
	 * 咨询或者产品的评论
	 * @param request
	 * @param response
	 */
	@RequestMapping("/commentList")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Page<PortalInfoComment> page = infoCommentService.pageQuery(request);
		for (PortalInfoComment comment : page.getData()) {
			Member member = memberService.getById(comment.getObjId());
			if(member != null){
				comment.setNickName(StringUtils.isNotBlank(member.getNickname()) ? member.getNickname() : member.getMobile());
			}
		}
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	/**
	 * 删除评论
	 * @param id
	 * @param response
	 */
	@RequestMapping("/deleteComment")
	public void deleteComment(@RequestParam("id")Long id,
			HttpServletResponse response){
		try {
			PortalInfoComment comment = infoCommentService.getById(id);
			infoCommentService.doDelete(id);
			portalInfoManager.subCommentCount(comment.getInfoId());
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("/portal/info/comment/deleteComment error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
}
